<template>
  <div class="fillcontain">
    <head-top></head-top>
    <div class="table_container">

      <el-button
        type="primary"
        @click="handleAdd">添加</el-button>


      <!--列表-->
      <el-table :data="DNames" highlight-current-row v-loading="loading" style="width: 100%;">
        <el-table-column type="index" width="60"></el-table-column>
        <el-table-column prop="name" label="名称" sortable></el-table-column>
        <el-table-column prop="eName" label="英文名称" sortable></el-table-column>
        <el-table-column prop="type" label="类型" sortable></el-table-column>
        <!--<el-table-column label="操作">-->
          <!--<template scope="scope">-->
            <!--<el-button-->
              <!--size="small"-->
              <!--@click="handleEdit(scope.row)">编辑</el-button>-->
            <!--<el-button-->
              <!--size="small"-->
              <!--type="danger"-->
              <!--@click="handleDelete(scope.$index, scope.row)">删除</el-button>-->
          <!--</template>-->
        <!--</el-table-column>-->
        <!--<el-table-column prop="age" label="name数量" width="100" sortable>-->
        <!--</el-table-column>-->
      </el-table>


      <el-dialog title="添加类型" :visible.sync="dialogFormVisible">
        <el-form :model="selectTable">
          <el-form-item label="设备名称" label-width="100px">
            <el-input v-model="selectTable.name"></el-input>
          </el-form-item>
          <el-form-item label="英文名" label-width="100px">
            <el-input v-model="selectTable.eName"></el-input>
          </el-form-item>
          <el-form-item label="设备类型" label-width="100px">
            <el-select v-model="selectIndex" :placeholder="selectMenu.label" @change="handleSelect">
              <el-option
                v-for="item in menuOptions"
                :key="item.value"
                :label="item.label"
                :value="item.index">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="createName">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
  import headTop from './head.vue'
  import { DeviceType, DeviceName, DeviceNameAdd, DeviceNameDel } from '@/api/api'
  export default {
    data () {
      return {
        filters: {
          name: ''
        },
        loading: false,
        DNames: [
        ],
        selectTable: {
          name: '',
          eName: ''
        },
        dialogFormVisible: false,
        menuOptions: [],
        selectMenu: {},
        selectIndex: null
      }
    },
    components: {
      headTop
    },
    created () {
      this.initData()
    },
    methods: {
      // 获取设备名称
      async initData () {
        this.loading = true
        this.selectTable.name = '' // 重置输入框内容
        try {
          DeviceName().then((res) => {
            this.DNames = res.data  // type 看下转换下，通过store获取
            this.loading = false
          })
        } catch (err) {
          console.log('获取数据失败', err)
        }
      },
      async getMenu () {
        this.menuOptions = []
        try {
          DeviceType().then((res) => {
            res.data.forEach((item, index) => {
              this.menuOptions.push({
                label: item.name,
                value: item.id,
                index
              })
            })
          })
        } catch (err) {
          console.log('获取设备名称失败', err)
        }
      },
      handleAdd () {
        this.getMenu()
        this.dialogFormVisible = true
      },
      handleSelect (index) {
        this.selectIndex = index
        this.selectMenu = this.menuOptions[index]
      },
      async createName () {
        this.dialogFormVisible = false
        let postData = {
          name: this.selectTable.name,
          eName: this.selectTable.eName,
          type: this.selectMenu.value
        }
        DeviceNameAdd(postData).then(res => {
          if (res.status === 201) {
            this.$message({
              type: 'success',
              message: '添加成功'
            })
            this.initData()
          } else {
            this.$message({
              type: 'error',
              message: '添加失败'
            })
          }
        }).catch(error => {
          this.$message({
            type: 'error',
            message: '添加失败'
          })
          console.log('类型添加失败', error)
        })
      },
      async handleDelete (index, row) {
        DeviceNameDel(row.id).then((res) => {
          this.$message({
            type: 'success',
            message: '删除成功'
          })
          this.DNames.splice(index, 1)
        }).catch(error => {
          this.$message({
            type: 'error',
            message: '删除失败'
          })
          console.log('类型删除失败', error)
        })
      }
    }
  }

</script>

<style scoped>
  .el-button {
    margin: 5px;
  }
</style>
